/*!
 * \file bullet_impl.cpp
 */

#include    "common.hpp"
#define     BULLET_IMAGE_PATH       "./gfx/bullet/bullet.pcx"
#define     BULLET_PLYR1_COLOUR     255, 64, 64
#define     BULLET_PLYR2_COLOUR     255,224, 64
#define     BULLET_PLYR3_COLOUR      64,255, 64
#define     BULLET_PLYR4_COLOUR      64, 96,255

int      BULLET::pool_index;
BULLET   BULLET::pool[NUM_BULLETS];

/******************************************************************************************/
/*! \brief updates all the bullets in the pool and check whether they've hit anything.
 */
void BULLET::update_all()
{
    int index;

    for (index = 0; index < BULLET::NUM_BULLETS; index++)
    {
        BULLET::pool[index].update();

        // collision checks with other entities will go here.
    }
}

/******************************************************************************************/
/*! \brief Updates this bullet's position.
 */
void BULLET::update()
{
    if (logic_clock > 0)
    {
        // move and update logic clock.
        x += x_speed;
        y += y_speed;
        logic_clock--;

        // hit any of the edges of the playfield? wrap.
        if (x < 0)                      x = APP::DISPLAY_WIDTH;
        if (y < 0)                      y = APP::DISPLAY_HEIGHT;
        if (x > APP::DISPLAY_WIDTH)     x = 0;
        if (y > APP::DISPLAY_HEIGHT)    y = 0;
    }
}

/******************************************************************************************/
/*! \brief Handle collision with another entity in the game world.
 */
void BULLET::on_hit(ENTITY &impactor)
{
    // uncomment this once PLAYER_SHIP and ASTEROID are implemented - it won't create the
    // type ids yet.
    /*
    if ( (typeid(impactor) == typeid(PLAYER_SHIP)) ||
         (typeid(impactor) == typeid(ASTEROID)) )
    {
        // todo - spawn explosions here

        // mark ourselves as inactive
        logic_clock = 0;
    }
    */
    return;
}

/******************************************************************************************/
/*! \brief Load our assets and reset all the BULLETs in the pool.
 */
void BULLET::init()
{
    anim = new ANIMATION(BULLET_IMAGE_PATH, 1);
    BULLET::reset_pool();
}

/******************************************************************************************/
/*! \brief Mark all the BULLETs in the pool as inactive.
 */
void BULLET::reset_pool()
{
    int index;

    for (index = 0; index < BULLET::NUM_BULLETS; index++)
    {
        BULLET::pool[index].logic_clock = 0;
    }
}

/******************************************************************************************/
/*! \brief Draw the current BULLET.
 */
void BULLET::paint(void)
{
    switch (fired_by)
    {
        case BULLET::PLYR1:
            glColor3ub(BULLET_PLYR1_COLOUR);
        break;

        case BULLET::PLYR2:
            glColor3ub(BULLET_PLYR2_COLOUR);
        break;

        case BULLET::PLYR3:
            glColor3ub(BULLET_PLYR3_COLOUR);
        break;

        case BULLET::PLYR4:
            glColor3ub(BULLET_PLYR4_COLOUR);
        break;
    }

    anim->paint(x, y, 0, radius, 0, 0);
    glColor3ub(255, 255, 255);
}
